<template>
	<div class="system-dept-dialog-container">
		<el-dialog :title="state.dialog.title" @close="closeDialog" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="ruleForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="路段编号：" prop="number" :rules="rules.number">
							<el-input :disabled="true" size="default"  placeholder="路段编号" style="max-width: 400px" v-model="ruleForm.number"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="路段名称：" prop="name" :rules="rules.name">
							<el-input size="default" placeholder="路段名称" style="max-width: 400px" v-model="ruleForm.name"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="所属区域：" prop="region_id" :rules="rules.status">
							<el-select clearable filterable  placeholder="所属区域" style="width: 400px;" v-model="ruleForm.region_id">
								<el-option v-for="item in selectData.regionData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item  label="所属道路：" prop="road_id" :rules="rules.road_id">
							<el-select clearable filterable  placeholder="所属道路" style="width: 400px;" v-model="ruleForm.road_id">
								<el-option v-for="item in selectData.roadData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="路段方向：" prop="direction" :rules="rules.direction">
							<el-select clearable  placeholder="路段方向" style="width: 400px;" v-model="ruleForm.direction">
								<el-option label="南北走向" :value="1"></el-option>
								<el-option label="东西走向" :value="2"></el-option>
								<el-option label="东北西南走向" :value="3"></el-option>
								<el-option label="西北东南走向" :value="4"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="启用状态：" prop="status" :rules="rules.status" >
							<el-select clearable  placeholder="启用状态" style="width: 400px;" v-model="ruleForm.status">
								<el-option label="停用" :value="0"></el-option>
								<el-option label="启用" :value="1"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDeptDialog">
import { reactive, ref,onMounted,watch,defineProps } from 'vue';
import {useBasicApi} from "/@/api/system/menu/index"
import { deptApi } from '/@/api/system/dept/index';
import {useRoleApi} from "/@/api/system/role/index"
import {  ElMessage, type FormRules } from 'element-plus'
const BasicApi = useBasicApi()
const depApi = deptApi()
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const roleApi = useRoleApi()
const selectData =reactive({
	regionData:[],
	roadData:[]
})
const props = defineProps(['tableData','disabled'])
const disabledFlag =ref<Boolean>(false)
const searchForm = ref({})
// dx定义start
	interface RuleForm {
		number:string,
		name:string,
		status:string,
		region_id:string,
		direction:string,
		road_id:string
	}
	const ruleForm = ref<RuleForm>({
		number:"",
		name:"",
		status:"",
		region_id:"",
		road_id:"",
		direction:""
	})

	const menuDialogFormRef = ref(null)

	const rules = reactive<FormRules<RuleForm>>({
		number:[
			{ required: true, message: '路段编号必填', trigger: 'blur' },
		],
		name:[
			{ required: true, message: '路段名称必填', trigger: 'blur' },
		],
		status:[
			{ required: true, message: '启用状态必填', trigger: 'blur' },
		],
		region_id:[
			{ required: true, message: '所属区域必填', trigger: 'blur' },
		],
		road_id:[
			{ required: true, message: '所属道路必填', trigger: 'blur' },
		],
		direction:[
			{ required: true, message: '路段方向必填', trigger: 'blur' },
		],
	})



// dx定义end

const getSelectData = (async ()=>{
	searchForm.value.search = `status:1`
	let res = await BasicApi.BasicTableApi(searchForm.value);
	if(res.isSuccess){
		selectData.regionData = res.returnData
	}
	let roadRes = await roleApi.RoleTableApi(searchForm.value);
	if(roadRes.isSuccess){
		selectData.roadData = roadRes.returnData
	}
})

onMounted(()=>{
	getSelectData()
})

watch(props,(newVal,oldVal)=>{
	ruleForm.value = newVal.tableData
	disabledFlag.value = newVal.disabled
})


// 定义变量内容
const deptDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		deptLevel: [] as string[], // 上级部门
		deptName: '', // 部门名称
		person: '', // 负责人
		phone: '', // 手机号
		email: '', // 邮箱
		sort: 0, // 排序
		status: true, // 部门状态
		describe: '', // 部门描述
	},
	deptData: [] as DeptTreeType[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = () => {
		state.dialog.title = '修改路段';
		state.dialog.submitTxt = '修 改';
	state.dialog.isShowDialog = true;
	
};
// 关闭弹窗
const closeDialog = () => {
	// menuDialogFormRef.value.resetFields()
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (async() => {
		const res = await depApi.editDep(ruleForm.value);
		if(res.isSuccess){
			ElMessage.success('修改成功')
			closeDialog();
			emit('refresh');
		}else{
			ElMessage.error(res.returnMsg)
		}
});


// 暴露变量
defineExpose({
	openDialog,
});
</script>
